#include "SavingWater.h"

SavingWater::SavingWater():Scene()
, P1Counter(0.0f)
, P2Counter(0.0f)
{
	CountingTime = CountTime;
	ChangingTime = ChangePage;
	P1Timer = PlayingTime;
	P2Timer = PlayingTime;
	GameEnd = false;

	Water1 = Object2D(70.0, 400.0, 0.0, true);
	Water2 = Object2D(70.0, 400.0, 0.0, true);
	Table = Object2D(800.0, 100.0, 0.0, true);
	EcoBackground = Object2D(800, 600, 0, true);
	IntroStartOff = Object2D(800.0, 600.0, 0.0, true);
	Bottle1 = Object2D(80.0, 400.0, 0.0, true);
	Bottle2 = Object2D(80.0, 400.0, 0.0, true);
	Tap1 = Object2D(100.0, 100.0, 0.0, true);
	Tap2 = Object2D(100.0, 100.0, 0.0, true);
	PlayerInfo1 = Object2D(150.0, 50.0, 0.0, true);
	PlayerInfo2 = Object2D(150.0, 50.0, 0.0, true);
	Player1Litre = Object2D(150.0, 50.0, 0.0, true);
	Player2Litre = Object2D(150.0, 50.0, 0.0, true);

	// Object's position
	EcoBackground.setPosition(400, 300);
	IntroStartOff.setPosition(400, 300);
	Bottle1.setPosition(200, 300);
	Bottle2.setPosition(500, 300);
	Water1.setPosition(200,700);
	Water2.setPosition(500,700);
	Table.setPosition(400,550);
	Tap1.setPosition(220, 60);
	Tap2.setPosition(520, 60);
	PlayerInfo1.setPosition(310, 250);
	PlayerInfo2.setPosition(610, 250);
	Player1Litre.setPosition(300, 300);
	Player2Litre.setPosition(600, 300);
}

void SavingWater::loadResource()
{
	// Images
	LoadTGA( &TableImage[0], "images/savingwater/table.tga", false);
	LoadTGA( &BackgroundImage[0], "images/savingwater/EcoBackground.tga", false);
	LoadTGA( &WaterImage[0], "images/savingwater/waterrising.tga", false);
	LoadTGA( &BottleImage[0], "images/savingwater/bottle.tga", false);
	LoadTGA( &TapImage[0], "images/savingwater/tap.tga", false);

	// Intro
	LoadTGA( &CountDownImage[0], "images/savingwater/intro1.tga", false);
	LoadTGA( &CountDownImage[1], "images/savingwater/intro2.tga", false);
	LoadTGA( &CountDownImage[2], "images/savingwater/ready.tga", false);
	LoadTGA( &CountDownImage[3], "images/savingwater/go.tga", false);

	// Game end
	LoadTGA( &GameEndImage[0], "images/savingwater/PlayerDraws.tga", false);
	LoadTGA( &GameEndImage[1], "images/savingwater/Player1Win.tga", false);
	LoadTGA( &GameEndImage[2], "images/savingwater/Player2Win.tga", false);

	//player info
	LoadTGA( &PlayerInfoImage[0], "images/savingwater/P1Timer.tga", false);
	LoadTGA( &PlayerInfoImage[1], "images/savingwater/P2Timer.tga", false);
	LoadTGA( &PlayerInfoImage[2], "images/savingwater/P1Litre.tga", false);
	LoadTGA( &PlayerInfoImage[3], "images/savingwater/P2Litre.tga", false);
}

void SavingWater::freeResource()
{
	for (int x = 0; x < 4; x++)
		freeTexture(&CountDownImage[x]);
	for (int x = 0; x < 3; x++)
		freeTexture(&GameEndImage[x]);
}

void SavingWater::reset()
{
	CountingTime = CountTime;
	ChangingTime = ChangePage;
	GameEnd = false;
	P1Timer = PlayingTime;
	P2Timer = PlayingTime;
	P1Counter = 0.0f;
	P2Counter = 0.0f;

	Water1 = Object2D(70.0, 400.0, 0.0, true);
	Water2 = Object2D(70.0, 400.0, 0.0, true);
	Table = Object2D(800.0, 100.0, 0.0, true);
	EcoBackground = Object2D(800, 600, 0, true);
	IntroStartOff = Object2D(800.0, 600.0, 0.0, false);
	Bottle1 = Object2D(80.0, 400.0, 0.0, true);
	Bottle2 = Object2D(80.0, 400.0, 0.0, true);
	Tap1 = Object2D(100.0, 100.0, 0.0,true);
	Tap2 = Object2D(100.0, 100.0, 0.0,true);
	PlayerInfo1 = Object2D(150.0, 50.0, 0.0, true);
	PlayerInfo2 = Object2D(150.0, 50.0, 0.0, true);
	Player1Litre = Object2D(150.0, 50.0, 0.0, true);
	Player2Litre = Object2D(150.0, 50.0, 0.0, true);

	// Object's position
	EcoBackground.setPosition(400, 300);
	IntroStartOff.setPosition(400, 300);
	Bottle1.setPosition(200, 300);
	Bottle2.setPosition(500, 300);
	Water1.setPosition(200,700);
	Water2.setPosition(500,700);
	Table.setPosition(400,550);
	Tap1.setPosition(220, 60);
	Tap2.setPosition(520, 60);
	PlayerInfo1.setPosition(310, 250);
	PlayerInfo2.setPosition(610, 250);
	Player1Litre.setPosition(300, 300);
	Player2Litre.setPosition(600, 300);
}

void SavingWater::draw()
{
	// Splash for starting of my mini game.
	if (CountingTime > 14)
	{
		// instruction
		IntroStartOff.drawPlane(CountDownImage[0].texID);
	}
	else if (CountingTime > 3)
	{
		IntroStartOff.drawPlane(CountDownImage[1].texID);
	}
	else if (CountingTime > 2)
	{
		IntroStartOff.drawPlane(CountDownImage[2].texID);
	}
	else if (CountingTime > 1)
	{
		IntroStartOff.drawPlane(CountDownImage[3].texID);
	}

	if (CountingTime == 0 && GameEnd == false)
	{
		//Background
		EcoBackground.drawPlane(BackgroundImage[0].texID);

		// Taps
		Tap1.drawPlane(TapImage[0].texID);
		Tap2.drawPlane(TapImage[0].texID);

		//Bottle
		Bottle1.drawPlane(BottleImage[0].texID);
		Bottle2.drawPlane(BottleImage[0].texID);

		//Water
		Water1.drawPlane(WaterImage[0].texID);
		Water2.drawPlane(WaterImage[0].texID);

		//Table
		Table.drawPlane(TableImage[0].texID);

		PlayerInfo1.drawPlane(PlayerInfoImage[0].texID);
		PlayerInfo2.drawPlane(PlayerInfoImage[1].texID);
		Player1Litre.drawPlane(PlayerInfoImage[2].texID);
		Player2Litre.drawPlane(PlayerInfoImage[3].texID);

		//Player 1 info
		glColor3f(1,0,0);
		printw(390,260,0,"%3.1f", P1Timer);
		printw(370,310,0,"%3.2f", P1Counter);

		//Player 2 info
		printw(690,260,0,"%3.1f", P2Timer);
		printw(670,310,0,"%3.2f", P2Counter);
	}

	if (P1Timer < P2Timer && GameEnd == true && WinCondt == 1)
	{
		IntroStartOff.drawPlane(GameEndImage[1].texID);
	}
	else if (P1Timer > P2Timer && GameEnd == true && WinCondt == 2)
	{
		IntroStartOff.drawPlane(GameEndImage[2].texID);
	}
	else if (P1Timer == P2Timer && GameEnd == true && WinCondt == 0)
	{
		IntroStartOff.drawPlane(GameEndImage[0].texID);
	}
}

void SavingWater::update()
{
	if (GameEnd == false)
	{
		if (CountingTime != 0)
		{
			float TimeEnd = 1 / RealTimeController::getInstance().getFPS();
			CountingTime -= TimeEnd;

			if (CountingTime < 0)
				CountingTime = 0;
		}
		if (CountingTime == 0 && GameEnd == false)
		{
			if (P1Counter != 100)
			{
				// Update game time
				float TimeEnd = 1 / RealTimeController::getInstance().getFPS();
				P1Timer += TimeEnd;

				if (P1Counter >= 100.0)
				{
					P1Counter = 100.0f;
					Water1.move(0.0,0.0);
				}
			}
			if (P2Counter != 100)
			{
				// Update game time
				float TimeEnd = 1 / RealTimeController::getInstance().getFPS();
				P2Timer += TimeEnd;

				if (P2Counter >= 100.0)
				{
					P2Counter = 100.0f;
					Water2.move(0.0,0.0);
				}
			}
		}

		if (P1Counter == 100 && P2Counter == 100)
		{
			if (P1Timer < P2Timer)
			{
				WinCondt = 1;
				GameEnd = true;
			}
			else if (P1Timer > P2Timer)
			{
				WinCondt = 2;
				GameEnd = true;
			}
			else if (P1Timer == P2Timer)
			{
				WinCondt = 0;
				GameEnd = true;
			}
		}
	}
	else
	{
		float TimeEnd = 1 / RealTimeController::getInstance().getFPS();
		ChangingTime -= TimeEnd;
		if (ChangingTime < 0)
		{
			(P1Timer < P2Timer) ? LeaderBoard::getInstance().P1_AddScore() : LeaderBoard::getInstance().P2_AddScore();
			SceneManager::getInstance().changeScene("Score_Board");
		}
	}
}			

void SavingWater::handleKeyboard(int key, int x, int y)
{
	switch ( key ) {
	case 27 : glutLeaveMainLoop();
	break;
	default : break;
	}
}
void SavingWater::handleKeyboardUp(int key, int x, int y)
{
	if (key == 'a' || key == 's' || key == 'd')
	{
		if(P1Counter != 100)
		{
			Water1.move(0.0, -3.0f);
			P1Counter ++;
		}
		else
		{
			Water1.move(0.0, 0.0);
		}
	}
	if (key == 'j' || key == 'k' || key == 'l')
	{
		if(P2Counter != 100)
		{
			Water2.move(0.0, -3.0f);
			P2Counter ++;
		}
		else
		{
			Water2.move(0.0, 0.0);
		}
	}
	// Skip to start the game
	if(key == ' ' && CountingTime != 0)
	{
		PlaySound("sounds/games/go.wav", NULL,SND_ASYNC);
		CountingTime = 3;
	}
}
void SavingWater::handleKeyboardSpecial(int key, int x, int y)
{
	switch(key)
	{
	case GLUT_KEY_F12:
		reset();
		break;
	}
}

void SavingWater::getMid(int &x, int &y)
{

}

void SavingWater::handleMouseClick(int button, int state, int x, int y)
{
	switch( button )
	{
	case GLUT_LEFT_BUTTON :
		break;
	case GLUT_RIGHT_BUTTON : 
		
		break;
	case GLUT_MIDDLE_BUTTON : break;
	}
}
void SavingWater::handleMouseMove(int x, int y)
{
}
